package cn.lb.login.service.impl;

import cn.lb.constant.LoginConstant;
import cn.lb.login.dto.LoginDTO;
import cn.lb.login.service.ILoginService;
import cn.lb.org.domain.Employee;
import cn.lb.org.mapper.EmployeeMapper;
import cn.lb.basic.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
@Service
public class LoginServiceImpl implements ILoginService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public R login(LoginDTO dto) {
        String username=dto.getUsername();
        String password=dto.getPassword();
        if (StringUtils.isEmpty(username)||StringUtils.isEmpty(password)){
            throw  new RuntimeException("用户名或密码错误");
        }
        Employee employee = employeeMapper.getByEmployee(username);
        if(ObjectUtils.isEmpty(employee)){
            throw  new RuntimeException("用户名或密码错误");
        }
        if(!password.equals(employee.getPassword())){
            throw  new RuntimeException("用户名或密码错误");
        }
        String token = UUID.randomUUID().toString();
        LoginConstant.LoginUserMap.put(token,employee);
        Map<String,Object > resultMap=new HashMap<>();
        resultMap.put("token",token);
        employee.setPassword("");
        resultMap.put("LoginUser",employee);

        return R.me().setData(resultMap);
    }
}
